package org.zjt.platform.springbootreactivedemo.service.impl;

import org.zjt.platform.springbootreactivedemo.entity.Person;
import org.zjt.platform.springbootreactivedemo.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.Objects;

/**
 * @author <a href="juntao.zhang@qq.com">juntao.zhang</a>
 * @Description:
 * @Package org.zjt.platform.springbootreactivedemo.service.impl
 * @date 2018/3/15 13:49
 * @see
 */
@Service
public class ReactiveUserDetailsServiceImpl implements ReactiveUserDetailsService {

    @Autowired
    private PersonRepository personRepository;

    /**
     * 根据用户名查询授权信息
     * @param name
     * @return
     */
    @Override
    public Mono<UserDetails> findByUsername(String name) {
        return Mono.create((monoSink -> {
            Person person = personRepository.getPersonByName(name);
            if (Objects.isNull(person))
                throw new UsernameNotFoundException(name);
            User userDetails = new User(person.getName(),person.getPassWord(),Arrays.asList(new SimpleGrantedAuthority("ADMIN"),new SimpleGrantedAuthority("ROLE_BOSS")));
            monoSink.success(userDetails);
        }));
    }
}
